package fons.navigator.views.island;

import java.awt.Color;
import java.util.List;
import java.util.Map;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;

import fons.model.ontology.ValueSpace;

public class MapColorManager {
	private final Map<ValueSpace, Color> colorMap = Maps.newHashMap();
	
	private final static List<Color> DEFAULT_COLORS = Lists.newArrayList(
		new Color(255, 0, 0),
		new Color(0, 255, 0),
		new Color(0, 0, 255),
		new Color(255, 255, 0),
		new Color(255, 0, 255),
		new Color(0, 255, 255),
		new Color(127, 0, 0),
		new Color(0, 127, 0),
		new Color(0, 0, 127),
		new Color(127, 127, 0),
		new Color(127, 0, 127),
		new Color(0, 127, 127)
	);
	private int counter = 0;
	
	public MapColorManager (VisualIsland island) {		
		for (ValueSpace vsp: island.getValueSpaces()) {
			this.addValueSpace(vsp);
		}
	}
	
	public void addValueSpace (ValueSpace vsp) {
		colorMap.put(vsp, createNewColor());
	}

	private Color createNewColor() {
		final Color result = DEFAULT_COLORS.get(counter);
		counter = (counter + 1) % DEFAULT_COLORS.size();
		return result;
	}
	
	public float redOf (ValueSpace vsp, float value) {
		final float result = value * colorMap.get(vsp).getRed() / 255.0f;
		return result < 1 ? result : 1;
	}
	
	public float greenOf (ValueSpace vsp, float value) {
		final float result = value * colorMap.get(vsp).getGreen() / 255.0f;
		return result < 1 ? result : 1;
	}
	
	public float blueOf (ValueSpace vsp, float value) {
		final float result = value * colorMap.get(vsp).getBlue() / 255.0f;
		return result < 1 ? result : 1;
	}
	
	public Color colorOf (ValueSpace vsp, float value) {
		final Color color = colorMap.get(vsp);
		float r = color.getRed() * value / 255.0f;
		if (r > 1) r = 1;
		float g = color.getGreen() * value / 255.0f;
		if (g > 1) g = 1;
		float b = color.getBlue() * value / 255.0f;
		if (b > 1) b = 1;
		
		return new Color(r, g, b);
	}
	
	public Color getColorOf (ValueSpace vsp) {
		return colorMap.get(vsp);
	}
	
	public void setColorOf (ValueSpace vsp, Color color) {
		colorMap.put(vsp, color);
	}
}
